#include "lib/svg.cpp"

string _vec2str(const vector<double> & vec) {
	stringstream ss;
	I(i, vec.size())
		ss << vec[i] << " ";
	return ss.str();
}

string _2str(double d) {
	stringstream ss;
	ss << d;
	return ss.str();
}

void chart(
		int width,
		int height,
		vector<double> x_data,
		vector<double> y_data,
		string x_label,
		string y_label)
{
	int label_height = 30;
	int font_size = 20;
	int line_width = 3;
	string axis_color = "#B0B0B0";
	string line_color = "#336600";
	string fill_color = "#CCFFCC";

	double x_min = x_data[0];
	double x_max = x_data[0];
	I(i, x_data.size()) {
		x_min = min(x_min, x_data[i]);
		x_max = max(x_max, x_data[i]);
	}
	double y_min = y_data[0];
	double y_max = y_data[0];
	I(i, y_data.size()) {
		y_min = min(y_min, y_data[i]);
		y_max = max(y_max, y_data[i]);
	}

	text tic_y0;
	tic_y0.content = _2str(y_min);
	tic_y0.x = _2str(label_height-(tic_y0.content.size()+1)*(font_size/2));
	tic_y0.y = _2str(height-label_height);
	tic_y0.font_size = _2str(font_size);
	tic_y0.font_family = "Times New Roman";
	tic_y0.fill = line_color;

	text tic_y1;
	tic_y1.content = _2str(y_max);
	tic_y1.x = _2str(label_height-(tic_y1.content.size()+1)*(font_size/2));
	tic_y1.y = _2str(label_height/2);
	tic_y1.font_size = _2str(font_size);
	tic_y1.font_family = "Times New Roman";
	tic_y1.fill = line_color;

	text tic_x0;
	tic_x0.content = _2str(x_min);
	tic_x0.x = _2str(label_height);
	tic_x0.y = _2str(height-label_height/2+2);
	tic_x0.font_size = _2str(font_size);
	tic_x0.font_family = "Times New Roman";
	tic_x0.fill = line_color;

	text tic_x1;
	tic_x1.content = _2str(x_max);
	tic_x1.x = _2str(width-tic_x1.content.size()*(font_size/2));
	tic_x1.y = _2str(height-label_height/2+2);
	tic_x1.font_size = _2str(font_size);
	tic_x1.font_family = "Times New Roman";
	tic_x1.fill = line_color;

	vector<double> points;
	I(i, x_data.size()) {
		double x = x_data[i];
		double y = y_data[i];
		y = height - label_height - (y - y_min) / (y_max - y_min) * (height-label_height);
		x = label_height + (x - x_min) / (x_max - x_min) * (width-label_height);
		points.push_back(x);
		points.push_back(y);
	}

	polygon pg;
	vector<double> points2;
	points2.push_back(label_height);
	points2.push_back(height - label_height);
	I(i, points.size()) points2.push_back(points[i]);
	points2.push_back(width);
	points2.push_back(height - label_height);
	pg.points = _vec2str(points2);
	pg.fill = fill_color;
	pg.stroke_width = "0";

	polyline pl;
	pl.points = _vec2str(points);
	pl.stroke = line_color;
	pl.fill = "none";
	pl.stroke_width = _2str(line_width);

	text label_x;
	label_x.x = _2str(label_height+(width-label_height)/2-x_label.size()*font_size/2/2);
	label_x.y = _2str(height-1);
	label_x.font_size = _2str(font_size);
	label_x.font_family = "Times New Roman";
	label_x.content = x_label;

	text label_y;
	label_y.x = _2str(label_height/2);
	label_y.y = _2str((height-label_height)/2 + y_label.size()*font_size/2/2);
	label_y.font_size = _2str(font_size);
	label_y.font_family = "Times New Roman";
	label_y.content = y_label;
	label_y.transform_origin = label_y.x+" "+label_y.y;
	label_y.transform = "rotate(-90)";

	line axis_x;
	axis_x.x1 = _2str(label_height);
	axis_x.y1 = _2str(height-label_height);
	axis_x.x2 = _2str(width);
	axis_x.y2 = _2str(height-label_height);
	axis_x.stroke = axis_color;
	axis_x.stroke_width = _2str(line_width);

	line axis_y;
	axis_y.x1 = _2str(label_height);
	axis_y.y1 = _2str(0);
	axis_y.x2 = _2str(label_height);
	axis_y.y2 = _2str(height-label_height);
	axis_y.stroke = axis_color;
	axis_y.stroke_width = _2str(line_width);

	SVG svg;
	svg.width = _2str(width*2);
	svg.height = _2str(height*2);
	svg += pg;
	svg += pl;
	svg += tic_x0;
	svg += tic_x1;
	svg += tic_y0;
	svg += tic_y1;
	svg += label_x;
	svg += label_y;
	svg += axis_x;
	svg += axis_y;
	cout << svg;
};

int main() {
	chart(300,200,
			vector<double>{1,2,3,4,5},
			vector<double>{70,85,80,90,95},
			"#exam", "grade");
}